<?php 
namespace app\recommend\taglib;

use think\template\TagLib;

class recommend extends Taglib
{

    // 标签定义
    protected $tags = [
        // 标签定义： attr 属性列表 close 是否闭合（0 或者1 默认1） alias 标签别名 level 嵌套层次
        // 文章
        'list'              =>  ['attr'=>'mark,order,limit,return','close'=>0],
    ];

    // 文章列表
    public function taglist($tag,$content) {
        if (empty($tag['mark'])) {//return为空 返回空
            return;
        }
        if (empty($tag['return'])) {
            $tag['return'] = '_data';
        }
        $str = '';
        $str .="<?php ";
        $str .="\${$tag['return']} = [];";
        $str .="\$_where = [];";
        // 解析 mark 限制
        if(isset($tag['mark'])){
            if ('$' == substr($tag['mark'],0,1)) {
                $tag['mark']   = $this->autoBuildVar($tag['mark']);
                $str .="\$_category_id = \\think\\Db::name('recommendcate') -> where(array('mark'=>array('eq',{$tag['mark']}))) -> value('id');";
            }else{
                $str .="\$_category_id = \\think\\Db::name('recommendcate') -> where(array('mark'=>array('eq','{$tag['mark']}'))) -> value('id');";
            }
            $str .="\$_where['category_id'] = array('eq',\$_category_id?:0);";
        }
        
        $str .="\$_m = new \app\\recommend\model\Recommend();";
        
        if (isset($tag['order'])) {
            $str .="\$_m -> order('{$tag['order']}');";
        }else{
            $str .="\$_m -> order('sort desc,id desc');";
        }
        $str .="\$_where['status'] = ['eq',1];";
        $str .="\$_m -> where(\$_where);";
        // 解析limit限制属性
        if (isset($tag['limit'])) {
            if ('$' == substr($tag['limit'],0,1)) {
                $tag['limit']   = $this->autoBuildVar($tag['limit']);
                $str .="\$_m -> limit({$tag['limit']});";
            }else{
                $str .="\$_m -> limit('{$tag['limit']}');";
            }
        }
        $str .="\${$tag['return']} = \$_m -> field(true) -> select();";
        $str .=" ?>";
        return $str;
    }

}